package com.sfchen.cloud.feign.api;

import com.google.common.collect.Lists;
import com.sfchen.cloud.dto.UserDto;
import com.sfchen.cloud.exception.BizException;
import com.sfchen.cloud.utils.JsonData;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/feign/user")
public class UserDetailApiController {

    private static final List<UserDto> USERS = Lists.newArrayList(
            new UserDto(1, "chen1", 20),
            new UserDto(2, "chen2", 22),
            new UserDto(3, "chen3", 23),
            new UserDto(4, "chen4", 24),
            new UserDto(5, "chen5", 25)
    );

    @GetMapping("/{userId}")
    public JsonData<UserDto> queryUserDetailById(@PathVariable("userId") Integer userId) {
        Optional<UserDto> userDto = USERS.stream().filter(u -> userId.equals(u.id())).findFirst();
        return userDto.map(JsonData::buildSuccess).orElseThrow(() -> new BizException("指定id user 信息不存在!"));
    }
}
